﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ContourBuilder.Components;

namespace ITAP.ContourBuilder.Manipulation
{
    // TODO: Проверить работу SerializedAction'а.
    public class SerializedAction : ContourAction
    {
        protected ContourContainer undoContainer;
        protected ContourContainer redoContainer;

        public SerializedAction(string name)
            : base(name)
        {
        }

        public override void Execute()
        {
            base.Execute();
            this.undoContainer = this.control.ContourContainer.Clone() as ContourContainer;
        }

        public override void Undo()
        {
            if (undoContainer != null)
            {
                redoContainer = this.control.ContourContainer.Clone() as ContourContainer;
                this.control.ContourContainer = undoContainer;
            }
        }


        public override void Redo()
        {
            if (redoContainer != null)
            {
                undoContainer = this.control.ContourContainer.Clone() as ContourContainer;
                this.control.ContourContainer = redoContainer;
            }            
        }

        public override bool CanUndo
        {
            get
            {
                return true;
            }
        }

        public override object Clone()
        {
            SerializedAction clone = (SerializedAction)Activator.CreateInstance(this.GetType(), new object[] { this.name });
            if (this.undoContainer != null)
                clone.undoContainer = this.undoContainer.Clone() as ContourContainer;
            if (this.redoContainer != null)
                clone.redoContainer = this.redoContainer.Clone() as ContourContainer;

            return clone;
        }

        public override void SaveToHistory()
        {
            this.redoContainer = this.control.ContourContainer.Clone() as ContourContainer;
            this.control.Manipulator.AddToHistory(this);
        }

    }
}
